package team.yidong.fenda.serviceImpl;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import team.yidong.fenda.entity.*;
import team.yidong.fenda.repository.*;
import team.yidong.fenda.service.AnswerService;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by xuanlong on 2017/10/31.
 */
@Service
public class AnswerServiceImpl implements AnswerService {
    @Autowired
    AnswerRepository answerRepository;
    @Autowired
    QuestionRepository questionRepository;
    @Autowired
    MasterRepository masterRepository;
    @Autowired
    HospitalRepository hospitalRepository;
    @Autowired
    MasterScoreRepository masterScoreRepository;
    @Autowired
    SysUserRepository sysUserRepository;
    @Autowired
    AnswerImgRepository answerImgRepository;

    @Override
    public JSONArray findAnswerByQuestionId(Integer questionId) {

        JsonConfig config = new JsonConfig();
        config.setExcludes(new String[]{"question", "hospitalPermissionSet", "masterScoreSet", "hospitalImgSet", "hospitalFieldList", "userSet", "masterList", "praiseUserSet", "answer"});
        List<Answer> answerList = answerRepository.findAnswerByQuestionId(questionId);

        return JSONArray.fromObject(answerList, config);
    }

    @Override
    public Answer findById(Integer answerId) {
        return answerRepository.findOne(answerId);
    }

    @Transactional
    @Override
    public void updateAnswer(Answer answer) {
        answerRepository.save(answer);
    }

    @Override
    public String openAnswerImg(Integer imgId) {
        return answerImgRepository.findOne(imgId).getImgUrl();
    }

    @Transactional
    @Override
    public void addQuestionAnswer(String sysUserName, Integer questionId, String answerContentText, List<String> stringList) throws ParseException {
        Question question = questionRepository.findOne(questionId);
        Master master = masterRepository.findByUserName(sysUserName);

        Answer answer = new Answer();
        Date date = new Date();
        SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String createDate = simpleFormat.format(date);
        answer.setCreatDate(createDate);
        answer.setAnswerTime(String.valueOf(date.getTime()));
        if (answerContentText != null && !answerContentText.equals("")) {
            answer.setInfo(answerContentText);
        }
        answer.setIsBlack("否");
        answer.setPraiseImg1("https://qny.liuxuanlong.xyz/QQ%E5%9B%BE%E7%89%8720171209151703.png");
        answer.setPraiseImg2("https://qny.liuxuanlong.xyz/QQ%E5%9B%BE%E7%89%8720171209151712.png");
        answer.setStarNum(0);
        answer.setListenTime(0);
        //如果是第一个回答的，设置question的answerTime
        if (question.getAnswerList().size() == 0) {

            long from = simpleFormat.parse(question.getCreatDate()).getTime();
            long to = simpleFormat.parse(createDate).getTime();
            int minutes = (int) ((to - from) / (1000 * 60));
            question.setAnswerTime(Integer.toString(minutes));
            question.setIsSolved("是");
        }
        Set<AnswerImg> answerImgSet = new HashSet<>();
        for (String str : stringList) {
            AnswerImg answerImg = new AnswerImg();
            answerImg.setImgUrl(str);
            answerImgSet.add(answerImg);
        }
        answer.setIsRead("0");
        answer.setAnswerImgSet(answerImgSet);
        answer.setQuestion(question);
        answer.setMaster(master);
        answerRepository.save(answer);
        if ("免费".equals(question.getWxOrder().getHandleStatus())) {

            MasterScore masterScore = new MasterScore();
            masterScore.setStatus("免费问题无积分");
            masterScore.setNum(0.0);
            masterScore.setCreatDate(simpleFormat.format(new Date()));
            masterScore.setMasterScoreAnswer(answer);
            masterScoreRepository.save(masterScore);
        } else {
            MasterScore masterScore = new MasterScore();
            masterScore.setStatus("初始积分");
            masterScore.setNum(0.0);

            masterScore.setCreatDate(simpleFormat.format(new Date()));
            masterScore.setMasterScoreAnswer(answer);
            masterScoreRepository.save(masterScore);
        }


    }
}
